package com.itboye.lottery.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.lottery.R;
import com.itboye.lottery.bean.CodeBean;
import com.itboye.lottery.fragment.home.shuangseqiu.ShuangSeQiuFragment;

import java.util.ArrayList;

/**
 * Created by wu on 2018/1/9.
 */

public class ShuangSeQiuCodeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private ArrayList<CodeBean> codeBeanArrayList;
    private boolean isRed;
    private Fragment activity;
    private Context context;

    public ShuangSeQiuCodeAdapter(Fragment activity, boolean isRed, ArrayList<CodeBean> codeBeanArrayList) {
        this.activity = activity;
        this.isRed = isRed;
        this.codeBeanArrayList = codeBeanArrayList;
    }

    public OnItemClickListener itemClickListener;

    public void setOnItemClickListener(ShuangSeQiuCodeAdapter.OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void clearAllStatus() {
        for (int i = 0; i < codeBeanArrayList.size(); i++) {
            if (codeBeanArrayList.get(i).isSelect()) {
                codeBeanArrayList.get(i).setSelect(false);
            }
        }
        notifyDataSetChanged();
    }

    public void randomCodeUpdate(ArrayList<CodeBean> randomCodeBeanArrayList) {
        clearAllStatus();//先清除原来的所有状态
        for (int i = 0; i < randomCodeBeanArrayList.size(); i++) {
            int randomCodeValue = randomCodeBeanArrayList.get(i).getCodeValue();
            for (int i1 = 0; i1 < codeBeanArrayList.size(); i1++) {
                int codeValue = codeBeanArrayList.get(i1).getCodeValue();
                if (randomCodeValue == codeValue) {
                    codeBeanArrayList.get(i1).setSelect(true);
                    randomCodeBeanArrayList.get(i).setSelect(true);
                }
            }

        }
        notifyDataSetChanged();
    }

    public int getSelectTypeCount(int type) {
        //0:红球
        // 1：红色托马
        //2：蓝球
        int count = 0;
        for (CodeBean codeBean : codeBeanArrayList) {
            if (codeBean.isSelect() && codeBean.getType() == type) {
                count++;
            }
        }
        return count;
    }

    public ArrayList<CodeBean> getSelectCodeArray() {
        //0:红球
        // 1：红色托马
        //2：蓝球
        ArrayList<CodeBean> arrayList = new ArrayList<>();
        int count = 0;
        for (CodeBean codeBean : codeBeanArrayList) {
            if (codeBean.isSelect()) {
                arrayList.add(codeBean);
            }
        }
        return arrayList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View v = LayoutInflater.from(context).inflate(R.layout.item_ball_code, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (isRed) {
            if (!codeBeanArrayList.get(position).isSelect()) {
                ((ViewHolder) holder).tv_code.setBackgroundResource(R.mipmap.hongqiuweixuanzhong);
                ((ViewHolder) holder).tv_code.setTextColor(activity.getResources().getColor(R.color.main_color));
            } else {

                ((ViewHolder) holder).tv_code.setBackgroundResource(R.mipmap.hongqiuxuanzhong);
                ((ViewHolder) holder).tv_code.setTextColor(activity.getResources().getColor(R.color.white));
            }
        } else {
            if (!codeBeanArrayList.get(position).isSelect()) {
                ((ViewHolder) holder).tv_code.setBackgroundResource(R.mipmap.lanqiuweixuanzhong);
                ((ViewHolder) holder).tv_code.setTextColor(activity.getResources().getColor(R.color.blue_75b4e2));
            } else {
                ((ViewHolder) holder).tv_code.setBackgroundResource(R.mipmap.lanqiuxuanzhong);
                ((ViewHolder) holder).tv_code.setTextColor(activity.getResources().getColor(R.color.white));
            }

        }
        ((ViewHolder) holder).tv_code.setText(codeBeanArrayList.get(position).getCodeText());
    }

    @Override
    public int getItemCount() {
        return codeBeanArrayList == null ? 0 : codeBeanArrayList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public ImageView img;
        public TextView tv_code;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_code = itemView.findViewById(R.id.tv_code);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (itemClickListener != null) {
                itemClickListener.onItemClick(v, getAdapterPosition());
            }
        }
    }
}
